<script lang="ts" setup>
import { Delete } from '@element-plus/icons-vue';
import { CascaderOption } from 'element-plus';
import { computed } from 'vue';
import { useStore } from 'vuex';
import { Storekey } from '../../../interface';
import { findpid } from '../../../util/code';

const p = defineProps<{ max?: number, label: number[], search: () => void }>()

const setlabel = computed(() => {
    return p.label
})

type E = {
    (e: 'change:label', value?: number): void
    (event: 'update:label', id: number): void
    (event: 'clear:label'): void
}
const store = useStore(Storekey)

const grouplabel = computed(() => {
    const data = store.state.label?.rows.filter((e) => e.type === "题目标签");
    return findpid(data ? data : []) as CascaderOption[]
});

const getlabel = computed(() => {
    return store.state.label?.rows.filter((e) => e.type === "题目标签") || []
})

const emit = defineEmits<E>()

function grouplabelclick(item: number) {
    emit('update:label', item)
}
function groupLabelClose(index: number) {
    emit('change:label', index)
}
function groupLabelClear() {
    emit('clear:label')
}


</script>

<template>
    <div class="aside-container-box">
        <!-- <el-scrollbar> -->
        <div class="aside-container-header">
            <div class="icon-text text-title">
                <el-icon class="text-primary">
                    <BrushFilled />
                </el-icon>
                <b class="font-size14"> 知识点筛选</b>
            </div>
            <div class="aside-container-header-content">
                <span class=" ">已选知识点({{ setlabel?.length }}):</span>
                <el-button class="font-size13" type="primary" :icon="Delete" plain size="default"
                    @click="groupLabelClear">清除</el-button>
            </div>
            <el-tag class="question-label" size="default" hit effect="plain" type="primary"
                v-for=" (tag, i) in setlabel" :key="i" closable @close="groupLabelClose(i)">
                {{ getlabel.find(e => e.ID === tag)?.name }}
            </el-tag>
        </div>
        <div class="aside-container-main">
            <div class="icon-text text-title">
                <el-icon class="text-primary">
                    <List />
                </el-icon>
                <b class="font-size14">知识点列表</b>
            </div>
            <el-collapse accordion class="aside-container-main-item font-size13" v-for="(group, i) in grouplabel">
                <el-collapse-item :name="i">
                    <template #title class="">
                        <el-text class="group-name font-size13" size="default">{{ group.name }}</el-text>
                    </template>
                    <el-button class="font-bold font-size13" bg text size="default"
                        :type="setlabel?.includes(Number(group.ID)) ? 'primary' : 'default'"
                        @click="grouplabelclick(Number(group.ID))">
                        {{ group.name }}
                    </el-button>
                    <el-button class="font-size13" bg text size="default"
                        :type="setlabel?.includes(Number(v.ID)) ? 'primary' : 'default'"
                        @click="grouplabelclick(Number(v.ID))"
                        v-for="v in findpid(getlabel, Number(group.ID)).filter(e => !e.children?.length)">
                        {{ v.name }}
                    </el-button>
                    <div class="font-size13"
                        v-for="v in findpid(getlabel, Number(group.ID)).filter(e => e.children?.length)">
                        <el-button class="font-bold font-size13" bg text size="default"
                            :type="setlabel?.includes(Number(v.ID)) ? 'primary' : 'default'"
                            @click="grouplabelclick(Number(v.ID))">
                            {{ v.name }}
                        </el-button>
                        <el-button class="font-size13" bg text size="default"
                            v-for="t in findpid(v.children || [], Number(v.ID)).filter(e => !e.children?.length)"
                            :type="setlabel?.includes(Number(t.ID)) ? 'primary' : 'default'"
                            @click="grouplabelclick(Number(t.ID))">
                            {{ t.name }}
                        </el-button>
                        <div class="font-size13"
                            v-for="t in findpid(v.children || [], Number(v.ID)).filter(e => e.children?.length)">
                            <el-button class="font-bold font-size13" bg text size="default"
                                :type="setlabel?.includes(Number(t.ID)) ? 'primary' : 'default'"
                                @click="grouplabelclick(Number(t.ID))">
                                {{ t.name }}
                            </el-button>
                            <el-button bg text size="default"
                                v-for="v in findpid(t.children || [], Number(t.ID)).filter(e => !e.children?.length)"
                                :type="setlabel?.includes(Number(v.ID)) ? 'primary' : 'default'"
                                @click="grouplabelclick(Number(v.ID))">
                                {{ v.name }}
                            </el-button>
                        </div>
                    </div>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
    <!-- </el-scrollbar> -->
</template>

<style scoped>
.aside-container-box {
    /* height: 100vh; */
    padding: var(--container-margin);
    --container-margin: 20px;
    --container-header-height: 20vh;
    --container-bg-color: #fff;
}

.aside-container-header {
    /* min-height: 20vh; */
    box-sizing: content-box;
    padding: 0 0 30px 0;
    border-bottom: 1px solid #dedede;
}

.aside-container-header-content {
    padding: 0 4px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 10px 0 10px 0;
}

.aside-container-main {
    margin-top: 20px;
}

.aside-container-main-item {
    border: none;
    /* padding-left: 10px; */
}

.group-name {
    color: rgba(51, 51, 51, 1);
    /* font-size: 14px; */
}

.font-bold {
    font-weight: bolder;
    /* --el-button-font-weight: 600; */
    color: var(--sass-color-blue) !important;
}

.aside-container-main-item .el-button {
    margin: 8px 8px 0 0;
}

/* 字体调整 */
.font-size13 {
    font-size: 13px;
    /* color: red !important; */
}

.font-size14 {
    font-size: 14px;
}
</style>

<style>
/* 有的显示器不显示下边框 */
.aside-container-main-item .el-collapse-item:last-child {
    margin-bottom: 0;
}

.aside-container-main-item .el-collapse-item__header {
    /* padding:0 10px; */
    padding-left: 10px;
    height: 100% !important;
    /* border: none; */
}

.aside-container-main-item .el-collapse-item__header:hover,
.aside-container-main-item .el-collapse-item__header:hover * {
    border-color: var(--sass-color-blue) !important;
    /* color:var(--sass-color-blue) !important; */
    /* color:#fff !important; */
}
</style>